package com.jader.activities;

import java.util.ArrayList;
import java.util.List;

import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.jader.adapter.MyPageAdapter;
import com.jader.fragment.CollectCustomFragment;
import com.jader.fragment.CollectGoodFragment;
import com.jader.fragment.CollectStoreFragment;
import com.zlp.jader.R;

public class CollectActivity extends FragmentActivity {
	TextView CollectGoods, CollectStores, CollectCustoms, tv_collect_top;
	ViewPager viewPager;// 页卡内容
	ImageView imageView, img_collect_back;
	List<Fragment> fragments;// Tab页面列表
	private int offset = 0;// 动画图片偏移量
	private int currIndex = 0;// 当前页卡编号
	private int bmpW;// 动画图片宽度
	private MyPageAdapter myPageAdapter;
	/** 页卡总数 **/
	private static final int pageSize = 3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.collect_and_order);
		initView();
	}

	/**
	 * 初始化视图
	 */
	private void initView() {
		img_collect_back = (ImageView) findViewById(R.id.top_actionbar_back);
		tv_collect_top = (TextView) findViewById(R.id.top_actiobar_text);
		tv_collect_top.setText("我的收藏");
		// 初始化Tab标签
		CollectGoods = (TextView) findViewById(R.id.Tab_one);
		CollectStores = (TextView) findViewById(R.id.Tab_two);
		CollectCustoms = (TextView) findViewById(R.id.Tab_three);
		CollectGoods.setText("商品收藏");
		CollectGoods.setTextColor(getResources().getColor(R.color.jader_theme));
		CollectStores.setText("店铺收藏");
		CollectCustoms.setText("帖子收藏");
		CollectGoods.setOnClickListener(new MyOnClickListener(0));
		CollectStores.setOnClickListener(new MyOnClickListener(1));
		CollectCustoms.setOnClickListener(new MyOnClickListener(2));
		img_collect_back.setOnClickListener(new MyOnClickListener(3));
		InitImageView();
		InitViewPager();
	}

	/**
	 * 初始化Viewpager页
	 */
	private void InitViewPager() {

		viewPager = (ViewPager) findViewById(R.id.Collect_And_order_viewpage);
		fragments = new ArrayList<Fragment>();
		fragments.add(new CollectGoodFragment());
		fragments.add(new CollectStoreFragment());
		fragments.add(new CollectCustomFragment());
		myPageAdapter = new MyPageAdapter(getSupportFragmentManager(),
				fragments);
		viewPager.setAdapter(myPageAdapter);
		viewPager.setCurrentItem(0);
		viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	/**
	 * 头标点击监听
	 */
	private class MyOnClickListener implements OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		public void onClick(View v) {
			switch (index) {
			case 0:
				break;
			case 1:

				break;
			case 2:
				break;
			case 3:
				finish();
			}
			viewPager.setCurrentItem(index);
		}
	}

	/** 为选项卡绑定监听器 */
	public class MyOnPageChangeListener implements OnPageChangeListener {

		int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
		int two = one * 2;// 页卡1 -> 页卡3 偏移量

		public void onPageScrollStateChanged(int index) {
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		public void onPageSelected(int index) {
			Animation animation = new TranslateAnimation(one * currIndex, one
					* index, 0, 0);// 显然这个比较简洁，只有一行代码。
			currIndex = index;
			animation.setFillAfter(true);// True:图片停在动画结束位置
			animation.setDuration(300);
			imageView.startAnimation(animation);
			switch (index) {
			case 0:
				reset();
				CollectGoods.setTextColor(getResources().getColor(
						R.color.jader_theme));

				break;
			case 1:
				reset();
				CollectStores.setTextColor(getResources().getColor(
						R.color.jader_theme));
				break;
			case 2:
				reset();
				CollectCustoms.setTextColor(getResources().getColor(
						R.color.jader_theme));
				break;
			default:
				break;
			}
		}
	}

	/** 初始化动画，这个就是页卡滑动时，下面的横线也滑动的效果，在这里需要计算一些数据 */
	private void InitImageView() {
		imageView = (ImageView) findViewById(R.id.Tab_Select);
		bmpW = BitmapFactory.decodeResource(getResources(),
				R.drawable.tab_selected_bg).getWidth();// 获取图片宽度
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;// 获取分辨率宽度
		offset = (screenW / pageSize - bmpW) / 2;// 计算偏移量--(屏幕宽度/页卡总数-图片实际宽度)/2
													// = 偏移量
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		imageView.setImageMatrix(matrix);// 设置动画初始位置
	}

	private void reset() {
		CollectCustoms.setTextColor(Color.BLACK);
		CollectGoods.setTextColor(Color.BLACK);
		CollectStores.setTextColor(Color.BLACK);
	}
}
